// GoSample project main.go
package main

import (
	"flag"
	"fmt"
	"github.com/kless/goconfig/config"
	"os"
	//"strings"
	"runtime"
	"tcpsocket"
)

var (
	configFile = flag.String("c", "config", "config file")
	debugModel = flag.Bool("debug", true, "enable debug mode")
	log        = NewLogging()
)

func main() {
	flag.Parse()
	if len(*configFile) == 0 /*|| strings.EqualFold(*configFile, "config")*/ {
		fmt.Println("EF service for golang V1.01 \n")
		flag.PrintDefaults()
		os.Exit(2)
	}
	SetLogLevel(*debugModel)
	c, err := config.ReadDefault(*configFile)
	if err != nil {
		log.Error(err.Error())
		os.Exit(2)
	}
	globleParams := map[string]string{
		"host": "127.0.0.1",
		"port": "3333",
	}
	databaseParams := map[string]string{
		"host":     "127.0.0.1",
		"port":     "3306",
		"dbname":   "ef_db_c",
		"username": "root",
		"password": "mysql50",
	}
	for k, _ := range globleParams {
		globleParams[k], err = c.String("globle", k)
		if err != nil {
			log.Error(err.Error())
			os.Exit(2)
		}
	}
	for k, _ := range databaseParams {
		databaseParams[k], err = c.String("database", k)
		if err != nil {
			log.Error(err.Error())
			os.Exit(2)
		}
	}

	runtime.GOMAXPROCS(runtime.NumCPU())

	log.Info("main start!")
	server := tcpsocket.NewEFServer(globleParams["host"], globleParams["port"])
	dsn := NewDSN(databaseParams)
	dbModel := NewModel(dsn)
	handler := new(EtHandler)
	handler.Debug = *debugModel
	handler.dbModel = dbModel
	server.Start(handler)
}
